#ifndef CBUSSCHEDULERXMLREADER_H
#define CBUSSCHEDULERXMLREADER_H

//************************************************************
//the format of the xml is as follows
//
//<?xml version="1.0" encoding="UTF-8" ?>
//<!--all the time is in mins-->
//    <BUSES>
//        <bus name="66" interval="10" begin="A" end="D"> <!--the begin station is A and end station is D-->
//            <A stationName="A" seqStart2End="1" seqEnd2Start="4" lat="11.11" lon="10.11" start2endFist="300" start2endLast="1320" end2startFirst="640" end2startLast="1320"></A>
//            <B stationName="B" seqStart2End="2" seqEnd2Start="3" lat="12.11" lon="20.11" start2endFist="315" start2endLast="1335" end2startFirst="625" end2startLast="1305"></B>
//            <C stationName="C" seqStart2End="3" seqEnd2Start="2" lat="13.11" lon="30.11" start2endFist="320" start2endLast="1340" end2startFirst="620" end2startLast="1300"></C>
//            <D stationName="D" seqStart2End="4" seqEnd2Start="1" lat="14.11" lon="40.11" start2endFist="327" start2endLast="1347" end2startFirst="613" end2startLast="1293"></D>
//        </bus>

//        <bus name="33" interval="15" begin="E" end="F"> <!--the begin station is E and end station is F-->
//            <E stationName="E" seqStart2End="1" seqEnd2Start="4" lat="20.11" lon="70.11" start2endFist="360" start2endLast="1200" end2startFirst="390" end2startLast="1230"></E>
//            <B stationName="B" seqStart2End="2" seqEnd2Start="3" lat="12.11" lon="20.11" start2endFist="365" start2endLast="1205" end2startFirst="385" end2startLast="1220"></B>
//            <D stationName="D" seqStart2End="3" seqEnd2Start="2" lat="14.11" lon="40.11" start2endFist="380" start2endLast="1220" end2startFirst="370" end2startLast="1205"></D>
//            <F stationName="F" seqStart2End="4" seqEnd2Start="1" lat="41.11" lon="80.11" start2endFist="390" start2endLast="1230" end2startFirst="360" end2startLast="1200"></F>
//        </bus>
//    </BUSES>
//
//***********************************************************

#include "tinyxml/tinystr.h"
#include "tinyxml/tinyxml.h"
#include "globalParameter.h"
#include <QMap>
#include "vector"

using namespace std;

class CBusSchedulerXMLReader
{
public:
    CBusSchedulerXMLReader();
    ~CBusSchedulerXMLReader();

    bool LoadFile(const char *filename);

    void GenerateIntermediaResult();

    // to get the station based hash table, which will help to find the rountin.
    QMultiMap<QString, BUS_INFO_STATION_BASED>& GetStationBasedResult() {return m_stationMap;}

    // to get the name based hash table, which will help to find all the station info based on the bus name
    QMap<QString, vector<BUS_INFO_NAME_BASED> >& GetNameBasedResult() {return m_busMap;}


private:
    bool ReadFirstBus();
    bool ReadNextBus();

    bool ReadFirstStation();
    bool ReadNextStation();

private:
    // all the times output is in mins
    void GetBusInfo(TiXmlElement* element, QString& busName, int& interval,\
                    QString& beginStation, QString &endStation);
    void GetStationInfo(TiXmlElement* element, QString& stationName, \
                        int& seqStart2End, int& seqEnd2Start,\
                        float& lat, float& lon, \
                        int& start2endFistTime, int&  start2endLastTime, \
                        int& end2startFirstTime, int& end2startLastTime);

private:
    TiXmlDocument *myDocument;
    TiXmlElement *previousBus;
    TiXmlElement *currentBus;

    TiXmlElement *previousStation;
    TiXmlElement *RootElement;

    // the pair is stationName, and BUS_INFO_INTERNAL
    // just use map to simulate a hash table, taking advantage of the sorting by key function
    QMultiMap<QString, BUS_INFO_STATION_BASED> m_stationMap;

    // the pair is busName, and a list of station info
    QMap<QString, vector<BUS_INFO_NAME_BASED> > m_busMap;

    // variables for store the temp info and later will use them to generate the hash table
    // bus info
    QString m_busName;
    int m_interval;
    QString m_beginStation;
    QString m_endStation;

    // station info
    QString m_stationName;
    int m_seqStartToEnd;
    int m_seqEndToStart;
    float m_lat;
    float m_lon;
    int m_start2endFistTime;
    int m_start2endLastTime;
    int m_end2startFirstTime;
    int m_end2startLastTime;
};

#endif // CBUSSCHEDULERXMLREADER_H
